﻿using Dapper;
using System;
using BCEntity.Common.Case.WebsiteCaseAttachment;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Case.WebsiteCaseAttachment
{
    /// <summary>
    /// 官网案例附件
    /// </summary>
    public class WebsiteCaseAttachmentData : IWebsiteCaseAttachmentData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitecaseattachment";
        private const string QUERY = @"SELECT WebsiteCaseAttachmentId,WebsiteCaseId,AttachmentUrl,ViewTimes FROM websitecaseattachment";
        public WebsiteCaseAttachmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网案例附件是否存在
        public async Task<bool> Exists(int websiteCaseAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCaseAttachmentId=?WebsiteCaseAttachmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseAttachmentId", websiteCaseAttachmentId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网案例附件
        public async Task<WebsiteCaseAttachmentEntity> Add(WebsiteCaseAttachmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitecaseattachment", new string[] { "WebsiteCaseId", "AttachmentUrl" });
            sql = string.Format("{0} {1} WHERE WebsiteCaseAttachmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", entity.WebsiteCaseId, DbType.Int32, size: 11);
            parameters.Add("?AttachmentUrl", entity.AttachmentUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCaseAttachmentEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网案例附件
        public async Task<WebsiteCaseAttachmentEntity> Update(WebsiteCaseAttachmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitecaseattachment", new string[] { "ViewTimes" }, new string[] { "WebsiteCaseAttachmentId" });
            sql = string.Format("{0} {1} WHERE WebsiteCaseAttachmentId=?WebsiteCaseAttachmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseAttachmentId", entity.WebsiteCaseAttachmentId, DbType.Int32, size: 11);
            parameters.Add("?ViewTimes", entity.ViewTimes, DbType.Int32, size: 11);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCaseAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网案例附件
        public async Task<bool> Delete(int websiteCaseAttachmentId)
        {
            string sql = @"DELETE FROM websitecaseattachment WHERE WebsiteCaseAttachmentId=?WebsiteCaseAttachmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseAttachmentId", websiteCaseAttachmentId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网案例附件列表
        public async Task<IEnumerable<WebsiteCaseAttachmentEntity>> GetList(int websiteCaseId)
        {
            string sql = string.Format("{0} WHERE WebsiteCaseId=?WebsiteCaseId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCaseId", websiteCaseId, DbType.Int32);
            return await this.databaseContext.Slave.QueryAsync<WebsiteCaseAttachmentEntity>(sql,parameters);
        }
        #endregion
        
    }
}



